﻿using System;
using System.IO;
using System.Windows.Forms;

namespace WpfApp1.Model
{
    class Tools
    {
        private InputData inputData;

        public InputData InputData { get => inputData; set => inputData = value; }

        public bool MoveFile(string originfile, string destfile)
        {
            try
            {
                FileInfo destinfo = new FileInfo(destfile);
                bool exist = destinfo.Exists;
                if (destinfo.Exists)
                {
                    string[] destArr = destfile.Split(".".ToCharArray());
                    string subfix = destArr[destArr.Length - 1];
                    if (destArr.Length > 1)
                    {
                        destArr[destArr.Length - 1] = "";
                    }

                    string[] dest_part_arr = destfile.Split(@"\".ToCharArray());
                    string oriName = dest_part_arr[dest_part_arr.Length - 1];

                    GlobalNotice.notice.Notice = "文件已经存在：" + destfile + "\r\n请对文件重新进行命名";
                    GlobalNotice.notice.Input = oriName;
                    GlobalNotice.notice.Dest_dictionary = destinfo.DirectoryName;
                    if (this.inputData == null)
                    {
                        inputData = new InputData
                        {
                            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen
                        };
                        inputData.ShowDialog();
                    }

                    if (GlobalNotice.notice.Is_jump || oriName.Equals(GlobalNotice.notice.Input))
                    {
                        GlobalNotice.notice.Is_jump = false;
                        string[] originArr = originfile.Split(@"\".ToCharArray());
                        throw new InvalidDataException("取消该文件处理：" + originArr[originArr.Length - 1]);
                    }

                    if (GlobalNotice.notice.Is_cancel)
                    {
                        GlobalNotice.notice.Is_cancel = false;
                        throw new OperationCanceledException("取消任务处理！");
                    }

                    // 文件重新命名
                    string destfile_new = destinfo.DirectoryName + @"\" + GlobalNotice.notice.Input;
                    // 再次执行移动
                    return MoveFile(originfile, destfile_new);
                }

                /*if (exist)
                {
                    FileSystem.DeleteFile(destfile, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                }*/

                //inputData.Close();
                new FileInfo(originfile).MoveTo(destfile);
            }
            catch (ArgumentNullException err)
            {
                MessageBox.Show(err.Message);
                return false;
            }

            return true;
        }

        // 时间戳格式化为日期文字
        public static string StampToDateTime(long stamp)
        {
            DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            DateTime now = start.AddSeconds(stamp);

            return now.ToString("yyyy/MM/dd HH:mm:ss");
        }
    }
}
